#include <stdio.h>
#include <stdlib.h>

static void before(void) __attribute__((constructor));
static void after(void) __attribute__((destructor));
static void middle(void);
static void before()
{
    system("chcp 65001"); //切换字符集
    system("cls");
}
static void after()
{
    system("echo.");
    system("pause");
}

const int MaxSize = 5;

typedef struct
{
    int num;
    char name[8];
    int age;
} Student;

typedef struct
{
    Student Students[MaxSize];
    int Length;
} SeqListStudent;

int main()
{
    printf("hello,world!\n");

    SeqListStudent students;

    int length = students.Length;
    char lengthStr[128];
    itoa(length, lengthStr, 10);
    //printf("The number 'num' is %d and the string 'str' is %s. \n", length, lengthStr);
    return 0;
}

//顺序表插入算法
void InserSeqList(SeqListStudent L, Student x, int i)
{
    int j;

    if (L.Length == MaxSize)
    {
        exit(1);
    }

    if (i < 1 || i > L.Length + 1)
    {
        exit(2);
    }

    for (j = L.Length; j >= i; j--)
    {
        L.Students[j] = L.Students[j - 1];
    }

    L.Students[i - 1] = x;

    L.Length++;
}

//顺序表删除算法
void DeleteSeqList(SeqListStudent L, int i)
{

    int j;
    if (i < 0 || i > L.Length)
    {
        exit(0);
    }

    for (j = i; j < L.Length; j++)
    {
        L.Students[j - 1] = L.Students[j];
    }

    L.Length--;
}

//顺序表按值定位
int LocateSeqList(SeqListStudent L, Student x)
{
    int i = 0;

    while ((i < L.Length) && (L.Students[i].num != x.num))
    {
        i++;
    }

    if (i < L.Length)
    {
        return i + 1;
    }
    else
    {
        return 0;
    }
}

//合并集合
void Merge(SeqListStudent A, SeqListStudent B, SeqListStudent C)
{
    int i = 0, j = 0, k = 0;

    while (i < A.Length && j < B.Length)
    {
        if (A.Students[i].age < B.Students[j].age)
        {
            C.Students[k++] = B.Students[i++];
        }
        else
        {
            C.Students[k++] = A.Students[i++];
        }
    }

    while (i < A.Length)
    {
        C.Students[k++] = A.Students[i++];
    }

    while (j < B.Length)
    {
        C.Students[k++] = B.Students[j++];
    }

    C.Length = k;
}

//单链表定义
typedef struct Node
{
    Student stu;       //数据域
    struct Node *next; //指针域
} StudentNode, *StudentLinkList;

//单链表的初始化：建立一个带头结点的空表
StudentLinkList InitiateLinkList()
{
    StudentLinkList head;
    head = (StudentLinkList)malloc(sizeof(StudentNode));
    head->next = NULL;

    return head;
}

//求表长：要访问到链路表所有的指针，需要逐个扫描指针
int LengthLinkList(StudentLinkList head)
{
    StudentNode *p = head;
    int cnt = 0;

    while (p->next != NULL)
    {
        p = p->next;
        cnt++;
    }

    return cnt;
}

//在单链表中查找第i个元素结点，找到返回其指针，否则返回空
StudentNode *GetLinkList(StudentLinkList head, int i)
{
    StudentNode *p; //p是工作指针
    p = head->next; //初始化时,p指向首节点

    int cnt = 1;

    while ((cnt < i) && (p != NULL))
    {
        p = p->next;
        cnt++;
    }

    if (cnt == i)
    {
        return p;
    }
    else
    {
        return NULL;
    }
}

//从链路表第一个元素起，判断当前值是否等于x，若是返回元素位置，若不是则继续查找，指导最后一个元素，没有返回0
int LocateLinkList(StudentLinkList head, Student x)
{
    StudentNode *p = head;
    p = p->next;
    int cnt = 1;
    while (p != NULL && p->stu.num != x.num)
    {
        p = p->next;
        cnt++;
    }

    if (p != NULL)
    {
        return cnt;
    }
    else
    {
        return 0;
    }
}

//